package com.xc.erp.modules.erp.controller.person;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xc.erp.common.annotation.Log;
import com.xc.erp.common.utils.SecurityUtils;
import com.xc.erp.modules.erp.domain.AjaxResult;
import com.xc.erp.modules.erp.domain.pojo.JshPerson;
import com.xc.erp.modules.erp.service.JshPersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 经手人信息
 *
 * @author wjw 2022/12/21
 * @DateTime: 2022/12/21 15:26
 */
@Api("经手人信息Api")
@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private JshPersonService jshPersonService;

    @ApiOperation("查询所有经手人信息")
    @GetMapping("list/{page}/{limit}")
    public AjaxResult list(@PathVariable Integer page,@PathVariable Integer limit,String name,String type){
        IPage<JshPerson> iPage = jshPersonService.personListPage(page,limit,name,type);
        return AjaxResult.success(iPage);
    }

    @Log(modul = "经手人模块",desc = "添加经手人信息")
    @ApiOperation("添加经手人信息")
    @PostMapping("add")
    public AjaxResult add(JshPerson jshPerson){
        jshPerson.setEnabled(true);
        jshPerson.setTenantId(SecurityUtils.getLoginUser().getJshUser().getTenantId());
        boolean result = jshPersonService.save(jshPerson);
        if (result) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @Log(modul = "经手人模块",desc = "修改经手人信息")
    @ApiOperation("修改经手人信息")
    @PutMapping("update")
    public AjaxResult update(JshPerson jshPerson){
        boolean result = jshPersonService.updateById(jshPerson);
        if (result) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @Log(modul = "经手人模块",desc = "删除经手人信息")
    @ApiOperation("删除经手人信息")
    @DeleteMapping("delete/{id}")
    public AjaxResult removeById(@PathVariable long id){
        boolean result = jshPersonService.removeById(id);
        if (result) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("删除失败");
        }
    }

    @Log(modul = "经手人模块",desc = "批量删除经手人信息")
    @ApiOperation("批量删除经手人信息")
    @DeleteMapping("batchDelById")
    public AjaxResult batchDelById(@RequestBody Long[] ids){
        boolean result = jshPersonService.removeBatchByIds(Arrays.asList(ids));
        if (result) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("删除失败");
        }
    }

    @Log(modul = "经手人模块",desc = "批量修改经手人状态")
    @ApiOperation("批量状态修改")
    @PutMapping("batchUpdateById/{status}")
    public AjaxResult batchUpdateById(@PathVariable boolean status,@RequestBody Long[] ids){
        boolean flag = jshPersonService.batchUpdateById(status,ids);
        if (flag) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @ApiOperation("检查名称是否存在")
    @GetMapping("checkPersonByName/{name}")
    public AjaxResult checkPersonByName(@PathVariable String name){
        boolean flag = jshPersonService.checkPersonByName(name);
        return AjaxResult.success().put("status",flag);
    }
}
